package study.algorithm.search;

import javax.xml.transform.Source;

public class BinarySearch {
    public BinarySearch() {
    }

    public static void main(String[] args) {
        System.out.println(binarySearch(new int[]{4, 56, 73, 84, 94, 333, 446}, 94));
    }

    public static int binarySearch(int[] arr, int target) {
        int min = 0;
        int max = arr.length - 1;

        while(min <= max) {
            int mid = (min + max) / 2;
            if (arr[mid] < target) {
                min = mid + 1;
            } else {
                if (arr[mid] <= target) {
                    return mid;
                }

                max = mid - 1;
            }
        }

        return -1;
    }
}
